package com.ssm.car.server.controller.parking;



import com.jee.ssm.common.support.Const;
import com.jee.ssm.common.utils.MD5;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.car.dao.model.Parking;
import com.ssm.car.rpc.api.parking.ParkingService;
import com.ssm.car.rpc.api.parkinglot.ParkinglotService;
import com.ssm.upms.dao.model.Account;
import com.ssm.upms.dao.model.AccountRole;
import com.ssm.upms.log.AdminControllerLog;
import com.ssm.upms.rpc.api.ssm.AccountRoleService;
import com.ssm.upms.rpc.api.ssm.AccountService;
import com.ssm.upms.rpc.api.ssm.CityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
* 停车场管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/parking")
public class ParkingController {


    /**
     * 进入停车场添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("parking:add")
    public String add(HttpServletRequest request,ModelMap model){
       /* ParamMap paramMap=new ParamMap(request);
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        if (account.getType() == 8){
            Parkingoffice parkingoffice = parkingofficeService.selectById(account.getInfoId());
            model.put("parkingoffice",parkingoffice);
        }
        model.put("type",account.getType());
        paramMap.put("parentId","1");
        model.put("provinces", cityService.chooseNext(paramMap));*/
        model.put("longId", UUIDFactory.getStringId());

        return "manager/parking/add";
    }


    /**
     * 进入停车场编辑页面
     * @param model 返回停车场的容器
     * @param id 停车场id
     * @return 编辑页面
     */
   /* @RequestMapping(value="/edit")
    @RequiresPermissions("parking:edit")
    public String edit(HttpServletRequest request,ModelMap model, @RequestParam String id) {
        Parking p = parkingService.selectById(id);
        if (p.getOfficeId()!=null &&!"".equals(p.getOfficeId())){
           p.setParkingoffice(parkingofficeService.selectById(p.getOfficeId()));
        }
        model.put("data",p);
        ParamMap paramMap=new ParamMap(request);
        paramMap.put("parentId",1);
        model.put("provinces", cityService.chooseNext(paramMap));
        Parking parking =parkingService.selectById(id);
        ParamMap p1=new ParamMap(request);
        p1.put("infoId",id);
        p1.put("type",4);
        List<Image>  imageList= imageService.selectByInfoId(p1);
        if(imageList.size()>0){
            model.put("imageList", imageList.get(0));
        }else{
            Image ig =new Image();
            ig.setId("0");
            ig.setUrl("0.jpg");
            model.put("imageList", ig);
        }
        paramMap.put("parentId",cityService.selectByName(parking.getProvince()).getId());
        model.put("city", cityService.chooseNext(paramMap));
        paramMap.put("parentId",cityService.selectByName(parking.getCity()).getId());
        model.put("county", cityService.chooseNext(paramMap));
        return "manager/parking/edit";
    }*/


    /**
     * 停车场添加
     * @param parking 带id的停车场对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("parking:add")
    @AdminControllerLog(description = "添加停车场" )
    public Tip save(Parking parking)  {

        try {
            parking.setSetTime(new Date());
            parkingService.insert(parking);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改停车场
     * @param parking 带id的停车场对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("parking:edit")
    @AdminControllerLog(description = "修改停车场" )
    public Tip update(Parking parking) {

        try {
            parkingService.updateById(parking);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除停车场
     * @param id 停车场id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("parking:delete")
    @AdminControllerLog(description = "删除停车场" )
    public Tip delete(@RequestParam String id) {
        try {
            parkingService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 停车场id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("parking:delete")
    @AdminControllerLog(description = "批量删除停车场" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            parkingService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
           e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找停车场
     * @param id 停车场id
     * @return 停车场对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("parking:list")
    public Parking find(@RequestParam String id) {

        return parkingService.selectById(id);
    }


    /**
     * 获取停车场列表 获取全部 不分页
     * @param request 请求参数
     * @return 停车场列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("parking:list")
    public String all(HttpServletRequest request,ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取停车场列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 停车场列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("parking:list")
    public String list(HttpServletRequest request,ModelMap modelMap,Integer page,Integer size) {
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap = new ParamMap(request);
        if(account.getType() == 6) {
            paramMap.put("parkingId", account.getInfoId());
        }else if(account.getType() == 8){
            paramMap.put("officeId",account.getInfoId());
        }
        modelMap.put("pageInfo",parkingService.list(paramMap,page,size));
        modelMap.put("type",account.getType());
        modelMap.putAll(new ParamMap(request));
        return "manager/parking/list";
    }

    /**
     * 全部停车场，地图
     * @return
     */
    @RequestMapping(value = "listAll")
    @ResponseBody
    public List<Parking> listAll(HttpServletRequest request){
        ParamMap paramMap = new ParamMap(request);
        Parking parking = new Parking();
        List<Parking> list = parkingService.listAll(paramMap);
        for(Parking park : list){
            parking.setId(park.getId());
            Long countParkTotal = parkingService.countParkTotal(parking);
            Long countPark = parkingService.countPark(parking);
            Long countAutoParkTotal = parkingService.countAutoParkTotal(parking);
            Long countAutoPark = parkingService.countAutoPark(parking);
            Long countOrdinaryParkTotal = parkingService.countOrdinaryParkTotal(parking);
            Long countOrdinaryPark = parkingService.countOrdinaryPark(parking);

            park.setCountParkTotal(countParkTotal);
            park.setCountPark(countPark);
            park.setCountAutoParkTotal(countAutoParkTotal);
            park.setCountAutoPark(countAutoPark);
            park.setCountOrdinaryParkTotal(countOrdinaryParkTotal);
            park.setCountOrdinaryPark(countOrdinaryPark);
        }
        return list;
    }

    /**
     * 停车场详情，地图
     * @param id
     * @return
     */
    @RequestMapping(value = "parkingDetail")
    public String parkingDetail(String id,ModelMap modelMap){
        Parking parking = new Parking();
        parking.setId(id);
        Parking park = parkingService.selectById(id);
        /*Long countPark = parkingService.countPark(parking);
        modelMap.put("countPark",countPark);
        Long countAutoPark = parkingService.countAutoPark(parking);
        modelMap.put("countAutoPark",countAutoPark);
        Long countOrdinaryPark = parkingService.countOrdinaryPark(parking);
        modelMap.put("countOrdinaryPark",countOrdinaryPark);*/
        Long countParkTotal = parkingService.countParkTotal(parking);
        Long countPark = parkingService.countPark(parking);
        Long countAutoParkTotal = parkingService.countAutoParkTotal(parking);
        Long countAutoPark = parkingService.countAutoPark(parking);
        Long countOrdinaryParkTotal = parkingService.countOrdinaryParkTotal(parking);
        Long countOrdinaryPark = parkingService.countOrdinaryPark(parking);

        modelMap.put("countParkTotal",countParkTotal);
        modelMap.put("countPark",countPark);
        modelMap.put("countAutoParkTotal",countAutoParkTotal);
        modelMap.put("countAutoPark",countAutoPark);
        modelMap.put("countOrdinaryParkTotal",countOrdinaryParkTotal);
        modelMap.put("countOrdinaryPark",countOrdinaryPark);
        modelMap.put("park",park);
        return "manager/parking/parkingDetail";
    }

    /**
     * 选择停车场
     * 2017-5-23 14:15:19 韩榕榕
     * @param session
     * @return 集合
     */
    @RequestMapping(value = "/chooseParking")
    public String chooseParking(HttpServletRequest request,HttpSession session,ModelMap map){
      Account account= (Account) session.getAttribute(Const.ACCOUNT);
      ParamMap paramMap=new ParamMap(request);
      if(account.getType()==6){
          paramMap.put("parkingId",account.getType());
      }
       map.put("data", parkingService.chooseParking(paramMap));
        return "manager/parking/chooseParking";
    }
    /**
     * 选择停车场
     * 2017-6-6 14:15:19 景玭玭
     * @param session
     * @return 集合
     */
    @RequestMapping(value = "/chooseParking2")
    public String chooseParking2(HttpServletRequest request,HttpSession session,ModelMap map){
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap=new ParamMap(request);
        if(account.getType()==6){
            paramMap.put("parkingId",account.getType());
        }
        map.put("data", parkingService.chooseParking(paramMap));
        return "manager/parking/chooseParking2";
    }

    /**
     * 2017年5月25日  闫增宝 查看停车场管理员的信息
     * @param parkingId 停车场id
     * @return 停车场管理员页面
     */
 /*   @RequestMapping(value = "/carAdminList")
    public  String carAdminList(@RequestParam String parkingId,ModelMap model){

        List<Account> accountList=accountService.selectByInfoId(parkingId);
        if(accountList.size()!=0){
            for (Account account:accountList){
                account.setParking(parkingService.selectById(account.getInfoId()));
            }
        }
        model.put("parkingId",parkingId);
        model.put("accountList",accountList);
        return "manager/parking/carAdminList";
    }*/

    /**
     * 2017年5月25日 闫增宝 进入停车场管理员添加页面
     * @param model 返回实体容器
     * @return 添加页面
     */
    @RequestMapping(value = "/addCarAdmin")
    public String addCarAdmin(ModelMap model,@RequestParam String parkingId){
        model.put("longId", UUIDFactory.getStringId());
        model.put("parkingId",parkingId);
        return "manager/parking/addCarAdmin";

    }


    /**
     * 2017年5月25日  闫增宝 停车场管理员的保存
     * @param account
     * @return 成功状态
     */
    @RequestMapping(value = "/saveCarAdmin")
    @ResponseBody
    public  Tip saveCarAdmin(Account account){
        try {
            account.setId(UUIDFactory.getStringId());
            account.setType(6);
            account.setPassword(MD5.md5(account.getPassword()));
  /*          account.setInfoId(account.getParkingId());*/
            accountService.insert(account);
            AccountRole accountRole=new AccountRole();
            accountRole.setId(UUIDFactory.getStringId());
            accountRole.setAccountId(account.getId());
            accountRole.setRoleId("4413459930049264285");
            accountRoleService.insert(accountRole);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"添加失败！");
        }
    }


    /**
     * 2017年5月25日  闫增宝
     * 根据accountId删除停车场管理员
     * @param id accountId
     * @return 成功状态
     */
    @RequestMapping(value="/deleteCarAdmin")
    @ResponseBody
    public Tip deleteCarAdmin(@RequestParam String id){
        try {
            accountRoleService.deleteByAccountId(id);
            accountService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"删除失败");
        }
    }

    /**
     * 2017年5月25日 闫增宝 恢复管理员登录
     * @param account 携带accountId
     * @return 成功状态
     */
    @RequestMapping(value="/allowLogin")
    @ResponseBody
    public Tip allowLogin(Account account){
        try {
            account.setStatus(1);
            accountService.updateById(account);
            return new Tip();
        }catch (Exception e){
            e.printStackTrace();
            return new Tip(1,"操作失败！");
        }
    }


    /**
     * 2017年5月25日 闫增宝 禁止管理员登录
     * @param account 携带accountId
     * @return 成功状态
     */
    @RequestMapping(value="/disableLogin")
    @ResponseBody
    public Tip disableLogin(Account account){
        try {
            account.setStatus(-1);
            accountService.updateById(account);
            return new Tip();
        }catch (Exception e){
            e.printStackTrace();
            return new Tip(1,"操作失败！");
        }
    }
    /**
     * 根据 id 删除图片
     * @param id 图片id
     * @return 成功状态
     */
   /* @RequestMapping(value="/deleteImage")
    public void deleteImage(@RequestParam String id) {
        try {
            imageService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }*/
    /**
     * 上传地图
     * @param parkingId
     * @param file
     * @param request
     * @return
     */
 /*   @RequestMapping(value = "/houseImages")
    @ResponseBody
    public Tip houseImages(@RequestParam String parkingId, @RequestParam(name = "house") MultipartFile file, HttpServletRequest request){
        try {
            String filename = file.getOriginalFilename();
            String name = UUIDFactory.getStringId() + "_" + System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));
            String url = "/images/parking/" + name;
            String realPath = request.getSession().getServletContext().getRealPath(url);
            File img = new File(realPath);
            if (!img.exists() && !img.isDirectory()) {
                img.mkdirs();
            }
            file.transferTo(img);
            Image image=new Image();
            image.setId(UUIDFactory.getStringId());
            image.setCreateTime(new Date());
            image.setType("4");
            image.setStatus("0");
            image.setUrl(url);
            image.setInfoId(parkingId);
            image.setIntro("停车场地图");
            imageService.deleteByInfoId(parkingId);
            parkingService.insertImg(image);
            return new Tip(image);

        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(2);
        }
    }*/

    //---------------------------- property -------------------------------

    @Resource
    private ParkingService parkingService;

    @Resource
    private CityService cityService;

    @Resource
    private AccountService accountService;

    @Resource
    private AccountRoleService accountRoleService;

    @Resource
    private ParkinglotService parkinglotService;

/*    @Resource
    private ImageService imageService;

    @Resource
    private ParkingofficeService parkingofficeService;*/

}
